package com.zhhs.galaxyott.httpVolley.base;

import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.AuthFailureError;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HurlStack;
import com.zhhs.galaxyott.httpVolley.parser.BaseParser;

/**
 * Json请求的实现类.
 * 
 * @author TG.1
 * @date 2015-10-09
 * 
 */
public class JsonRequestImpl {

    protected static final String TAG = "JsonRequestImpl";

    // private Context mContext;
    private final RequestQueue requestQueue;

    /**
     * Constructor.
     */
    public JsonRequestImpl(Context context) {

        // this.mContext = context;
        // this.requestQueue = Volley.newRequestQueue(context);

        int maxMemory = (int) Runtime.getRuntime().maxMemory();
        int cacheSize = maxMemory / 8;

        HurlStack stack = new HurlStack();

        // OkHttpStack okHttpStack = new OkHttpStack(new OkHttpClient());
        // this.requestQueue = MyVolley.newRequestQueue(context, okHttpStack,
        // cacheSize);
        this.requestQueue = MyVolleyforCache.newRequestQueue(context, stack, cacheSize);

    }

    /**
     * 网络请求.
     * 
     * @param url
     *            接口地址.
     * @param jsonStr
     *            待上传的Json数据.
     * @param params
     *            参数.
     * @param responseListener
     *            请求结果监听器.
     * @hide
     */
    public void request(String url, String jsonStr, BaseParser parser, final Map<String, String> params,
            final ResponseListener responseListener) {
        if (TextUtils.isEmpty(jsonStr)) {
            if (responseListener != null) {
                responseListener.onError(new VolleyError("invalid requesting params."));
            }
            return;
        }

        JSONObject json = null;

        try {
            json = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
            if (responseListener != null) {
                responseListener.onError(new VolleyError("invalid json"));
            }
            return;
        }

        request(url, json, parser, params, responseListener);
    }

    public void requestArray(String url, String jsonStr, BaseParser parser, final Map<String, String> params,
            final ResponseListener responseListener) {
        if (TextUtils.isEmpty(jsonStr)) {
            if (responseListener != null) {
                responseListener.onError(new VolleyError("invalid requesting params."));
            }
            return;
        }

        JSONArray jsonArray = null;

        try {
            jsonArray = new JSONArray(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
            if (responseListener != null) {
                responseListener.onError(new VolleyError("invalid json"));
            }
            return;
        }

        requestArray(url, jsonArray, parser, params, responseListener);
    }

    /**
     * 网络请求.
     * 
     * @param url
     *            接口地址.
     * @param json
     *            待上传的Json数据.
     * @param params
     *            参数.
     * @param responseListener
     *            请求结果监听器.
     * @hide
     */
    public synchronized Request request(String url, JSONObject json, BaseParser parser,
            final Map<String, String> params, final ResponseListener responseListener) {
        if (json == null) {
            if (responseListener != null) {
                responseListener.onError(new VolleyError("invalid json"));
            }
            return null;
        }

        JsonStringRequest jsonRequest = new JsonStringRequest(url, json.toString(), parser, responseListener) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                if (params != null) {
                    return params;
                }
                return super.getParams();
            }
        };

        jsonRequest.setRetryPolicy(new DefaultRetryPolicy(18000, 5, 0.01f));
        // 如果需要缓存Volley此为必须
        jsonRequest.setShouldCache(true);

        requestQueue.add(jsonRequest);

        return jsonRequest;
    }

    public synchronized Request requestArray(String url, JSONArray json, BaseParser parser,
            final Map<String, String> params, final ResponseListener responseListener) {
        if (json == null) {
            if (responseListener != null) {
                responseListener.onError(new VolleyError("invalid json"));
            }
            return null;
        }

        JsonStringRequest jsonRequest = new JsonStringRequest(url, json.toString(), parser, responseListener) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                if (params != null) {
                    return params;
                }
                return super.getParams();
            }
        };

        jsonRequest.setRetryPolicy(new DefaultRetryPolicy(18000, 5, 0.01f));
        // 如果需要缓存Volley此为必须
        jsonRequest.setShouldCache(true);

        requestQueue.add(jsonRequest);

        return jsonRequest;
    }

    /**
     * 停止所有请求.
     * 
     * @hide
     */
    public void stopAll() {
        if (requestQueue == null) {
            return;
        }

        requestQueue.cancelAll(new RequestQueue.RequestFilter() {
            @Override
            public boolean apply(Request<?> request) {
                return true;
            }
        });

        requestQueue.stop();
    }

}
